package com.customization.unionpay.market.dao;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormUtil;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;

public class MarketAdvanceCtrlDao {

    public Logger log = LoggerFactory.getLogger(MarketAdvanceCtrlDao.class);

    public boolean insert(JSONObject jsonObject, int requestid, String tableName){
//        StringBuilder sb = new StringBuilder();
//        sb.append("select requestid,lastoperatetime from workflow_requestbase where currentnodetype=3 and requestid=")
//                .append(requestid);
        RecordSet rs = new RecordSet();
//        rs.executeSql(sb.toString());
//        JSONObject zhrqJsom = new JSONObject();
//        while (rs.next()){
//            zhrqJsom.put("zhrq",rs.getInt("lastoperatetime"));
//        }
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(com.customization.unionpay.market.util.FormUtil.Advance_CTRL)
                .append("(ysxm,yskm,zhdw,ysqjn,je,bzydjh,zhdjh,yskzq,zhdjlx,bzr,lyxt,sm,tdrbm,szgs,jjsx,event,lylc,lyywbm)");
        if(jsonObject!=null){
            sql.append(" values ")
                    .append("(")
                    .append("'").append(jsonObject.getString("budgetItem")).append("'").append(",")
                    .append("'").append(jsonObject.getString("budgetAccount")).append("'").append(",")
                    .append("'").append(jsonObject.getString("exeUnit")).append("'").append(",")
                    .append("'").append(jsonObject.getString("budgetPeriod")).append("'").append(",")
                    .append("'").append(jsonObject.getString("amount")).append("'").append(",")
                    .append("'").append(jsonObject.getString("occDocNo")).append("'").append(",")
                    .append("'").append(jsonObject.getString("exeDocNo")).append("'").append(",")
                    .append("'").append(jsonObject.getString("budgetPostingPeriod")).append("'").append(",")
                    .append("'").append(jsonObject.getString("exeDocType")).append("'").append(",")
                    .append("'").append(jsonObject.getString("claimant")).append("'").append(",")
                    .append("'").append(jsonObject.getString("sourceSystem")).append("'").append(",")
                    .append("'").append(jsonObject.getString("remark")).append("'").append(",")
                    .append("'").append(jsonObject.getString("reimbursementDept")).append("'").append(",")
                    .append("'").append(jsonObject.getString("reimbursementCompany")).append("'").append(",")
                    .append("'").append(jsonObject.getString("economicEvent")).append("'").append(",")
                    .append("'").append(jsonObject.getString("event")).append("'").append(",")
                    .append("'").append(requestid).append("'").append(",")
                    .append("'").append(tableName).append("'")
                    .append(")");
            log.info(sql);
            boolean result = rs.execute(sql.toString());
            if(!result){
                log.error("预算控制中间表数据插入失败"+jsonObject.toJSONString());
                return false;
            }
            return result;
        }
        return false;
    }
    public List<JSONObject> selectByBdh(String bdh){
        StringBuilder sql = new StringBuilder();
        sql.append("select id from ").append(FormUtil.APPLICATION_ADJUSTMENT)
                .append(" where bdh = '").append(bdh).append("'");
        RecordSet rs = new RecordSet();
        List<JSONObject> results = new ArrayList<>();
        try {
            rs.execute(sql.toString());
            while (rs.next()){
                JSONObject json = new JSONObject();
                json.put("requestid",rs.getInt("requestid"));
                results.add(json);
            }
            return results;
        }catch (Exception e){
            log.error("查询清算资金明细失败");
        }
        return null;
    }
}
